package com.industry.framework.common.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.net.URLDecoder;

/**
 * @author jsy
 * @description:客户端工具类
 * @date 2025/1/11 19:17
 */
public class ServletUtil {

    private static final String X_REAL_IP = "X-Real-IP";
    private static final String X_FORWARDED_FOR = "X-Forwarded-For";
    private static final String X_FORWARDED_FOR_SPLIT_SYMBOL = ",";

    /**
     * @description: 获取String参数
     * @author: jsy
     * @time: 2025/1/11 19:42
     */
    public static String getParameter(String name) {
        HttpServletRequest request = getRequest();
        if (request != null) {
            String requestHeaderResult = request.getHeader(name);
            if (requestHeaderResult != null) {
                requestHeaderResult = URLDecoder.decode(requestHeaderResult);
            }
            return request.getParameter(name) == null ? requestHeaderResult : request.getParameter(name);
        }
        return null;
    }

    /**
     * @description: 获取String参数
     * @author: jsy
     * @time: 2025/1/11 19:41
     */
    public static String getParameter(String name, String defaultValue) {
        return name == null ? defaultValue : name;
    }

    /**
     * @description: 获取int参数
     * @author: jsy
     * @time: 2025/1/11 19:41
     */
    public static int getParameterToInt(String name) {
        return Convert.toInt(getParameter(name));
    }

    /**
     * @description: 获取int参数
     * @author: jsy
     * @time: 2025/1/11 19:41
     */
    public static int getParameterToInt(String name, int defaultValue) {
        return Convert.toInt(getParameter(name), defaultValue);
    }

    /**
     * @description: 获取long参数
     * @author: jsy
     * @time: 2025/1/11 19:41
     */
    public static Long getParameterToLong(String name, Long defaultValue) {
        return Convert.toLong(getParameter(name), defaultValue);
    }

    /**
     * @description: 获取request
     * @author: jsy
     * @time: 2025/1/11 19:41
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = getRequestAttributes();
        return attributes == null ? null : attributes.getRequest();
    }

    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

    /**
     * @description: 获取实际请求地址
     * @author: jsy
     * @time: 2025/1/11 19:41
     */
    public static String getRemoteIp(HttpServletRequest request) {
        String xForwardedFor = request.getHeader(X_FORWARDED_FOR);
        if (!StrUtil.isBlank(xForwardedFor)) {
            return xForwardedFor.split(X_FORWARDED_FOR_SPLIT_SYMBOL)[0].trim();
        }
        String nginxHeader = request.getHeader(X_REAL_IP);
        return StrUtil.isBlank(nginxHeader) ? request.getRemoteAddr() : nginxHeader;
    }
}
